//
//  HPExpert.m
//
//  Created by 啊  on 16/4/24
//  Copyright (c) 2016 __MyCompanyName__. All rights reserved.
//

#import "HPExpert.h"


NSString *const kHPExpertEName = @"eName";
NSString *const kHPExpertEMaxAge = @"eMaxAge";
NSString *const kHPExpertEInfo = @"eInfo";
NSString *const kHPExpertEMinAge = @"eMinAge";
NSString *const kHPExpertEBirthYear = @"eBirthYear";
NSString *const kHPExpertEProfession = @"eProfession";
NSString *const kHPExpertEHeadUrl = @"eHeadUrl";
NSString *const kHPExpertEId = @"eId";


@interface HPExpert ()

- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict;

@end

@implementation HPExpert

@synthesize eName = _eName;
@synthesize eMaxAge = _eMaxAge;
@synthesize eInfo = _eInfo;
@synthesize eMinAge = _eMinAge;
@synthesize eBirthYear = _eBirthYear;
@synthesize eProfession = _eProfession;
@synthesize eHeadUrl = _eHeadUrl;
@synthesize eId = _eId;


+ (instancetype)modelObjectWithDictionary:(NSDictionary *)dict
{
    return [[self alloc] initWithDictionary:dict];
}

- (instancetype)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];
    
    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
            self.eName = [self objectOrNilForKey:kHPExpertEName fromDictionary:dict];
            self.eMaxAge = [[self objectOrNilForKey:kHPExpertEMaxAge fromDictionary:dict] doubleValue];
            self.eInfo = [self objectOrNilForKey:kHPExpertEInfo fromDictionary:dict];
            self.eMinAge = [[self objectOrNilForKey:kHPExpertEMinAge fromDictionary:dict] doubleValue];
            self.eBirthYear = [self objectOrNilForKey:kHPExpertEBirthYear fromDictionary:dict];
            self.eProfession = [self objectOrNilForKey:kHPExpertEProfession fromDictionary:dict];
            self.eHeadUrl = [self objectOrNilForKey:kHPExpertEHeadUrl fromDictionary:dict];
            self.eId = [[self objectOrNilForKey:kHPExpertEId fromDictionary:dict] doubleValue];

    }
    
    return self;
    
}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    [mutableDict setValue:self.eName forKey:kHPExpertEName];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eMaxAge] forKey:kHPExpertEMaxAge];
    [mutableDict setValue:self.eInfo forKey:kHPExpertEInfo];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eMinAge] forKey:kHPExpertEMinAge];
    [mutableDict setValue:self.eBirthYear forKey:kHPExpertEBirthYear];
    [mutableDict setValue:self.eProfession forKey:kHPExpertEProfession];
    [mutableDict setValue:self.eHeadUrl forKey:kHPExpertEHeadUrl];
    [mutableDict setValue:[NSNumber numberWithDouble:self.eId] forKey:kHPExpertEId];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

- (NSString *)description 
{
    return [NSString stringWithFormat:@"%@", [self dictionaryRepresentation]];
}

#pragma mark - Helper Method
- (id)objectOrNilForKey:(id)aKey fromDictionary:(NSDictionary *)dict
{
    id object = [dict objectForKey:aKey];
    return [object isEqual:[NSNull null]] ? nil : object;
}


#pragma mark - NSCoding Methods

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super init];

    self.eName = [aDecoder decodeObjectForKey:kHPExpertEName];
    self.eMaxAge = [aDecoder decodeDoubleForKey:kHPExpertEMaxAge];
    self.eInfo = [aDecoder decodeObjectForKey:kHPExpertEInfo];
    self.eMinAge = [aDecoder decodeDoubleForKey:kHPExpertEMinAge];
    self.eBirthYear = [aDecoder decodeObjectForKey:kHPExpertEBirthYear];
    self.eProfession = [aDecoder decodeObjectForKey:kHPExpertEProfession];
    self.eHeadUrl = [aDecoder decodeObjectForKey:kHPExpertEHeadUrl];
    self.eId = [aDecoder decodeDoubleForKey:kHPExpertEId];
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{

    [aCoder encodeObject:_eName forKey:kHPExpertEName];
    [aCoder encodeDouble:_eMaxAge forKey:kHPExpertEMaxAge];
    [aCoder encodeObject:_eInfo forKey:kHPExpertEInfo];
    [aCoder encodeDouble:_eMinAge forKey:kHPExpertEMinAge];
    [aCoder encodeObject:_eBirthYear forKey:kHPExpertEBirthYear];
    [aCoder encodeObject:_eProfession forKey:kHPExpertEProfession];
    [aCoder encodeObject:_eHeadUrl forKey:kHPExpertEHeadUrl];
    [aCoder encodeDouble:_eId forKey:kHPExpertEId];
}

- (id)copyWithZone:(NSZone *)zone
{
    HPExpert *copy = [[HPExpert alloc] init];
    
    if (copy) {

        copy.eName = [self.eName copyWithZone:zone];
        copy.eMaxAge = self.eMaxAge;
        copy.eInfo = [self.eInfo copyWithZone:zone];
        copy.eMinAge = self.eMinAge;
        copy.eBirthYear = [self.eBirthYear copyWithZone:zone];
        copy.eProfession = [self.eProfession copyWithZone:zone];
        copy.eHeadUrl = [self.eHeadUrl copyWithZone:zone];
        copy.eId = self.eId;
    }
    
    return copy;
}


@end
